package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
  @Autowired
  private UserMapper userMapper;

    /**
     * 通过用户名查找用户
     * @param username
     * @return
     */
    public User LoadUserByname(String username) {
        return userMapper.FindByName(username);
    }

  /**
   * 查找用户角色
   * @param id
   * @return
   */
  public HashSet<Role> loadRolesByUserId(Integer id) {
         HashSet<Role> roles =  userMapper.findByid(id);

         roles.forEach(role -> {
           //获取权限并设置
           HashSet<Permission> permissions = userMapper.FindPermissionsById(role.getId());
           role.setPermissions(permissions);
           //获取菜单
           LinkedHashSet<Menu> menus = userMapper.findMenusById(role.getId());
           role.setMenus(menus);
         });

         return roles;
  }
}
